import fetch from '../config/fetch'
//判断是否是微信端
export const isWeiXin = () => {
							let ua = window.navigator.userAgent.toLowerCase();
							if (ua.match(/MicroMessenger/i) == 'micromessenger') {
								return true;
							} else {
								return false;
							}
						}

//微信url
export const towxurl = (url) =>{
							return 'http://'+window.location.host+'/'+url
						}
export const tovueurl = (url) =>{
	var allpath=window.location.href
	// alert(allpath);
	// debugger;
	
	if(allpath.indexOf('html?') != -1){
		allpath = decodeURIComponent(allpath)
		allpath = allpath.replace('html?','html#')
		var hashpath = '#'+allpath.split('#')[1]
	}else{
		// alert(666);
		// debugger
		var hashpath = window.location.hash
	}
	var pos=allpath.indexOf(hashpath);
	var rooturl=allpath.substring(0,pos)
	return rooturl+url
}
//从url中获取指定的参数
export const GetQueryString = (name) => {
								    let after = window.location.hash.split("?")[1];
								    if(after){
								        let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
								        let r = after.match(reg);
								        if(r != null)
								        {
								            return  decodeURIComponent(r[2]);
								        }
								        else
								        {
								            return null;
								        }
								    }else{
									    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
									    let r = window.location.search.substr(1).match(reg);
									    // console.log(r)
									    
									    if (r != null) {
									        return unescape(r[2]);
									    }
									    return null;
								    }
								}
//退出登录
export const logout = () => fetch('/user/logout',{},'POST')
//设置cookie
export const setCookie = function(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setTime(Number(exdate) + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//获取cookie
export const getCookie = function(c_name) {
	if(document.cookie.length > 0) {
		var c_start = document.cookie.indexOf(c_name + "=");//获取字符串的起点
		if(c_start != -1) {
			c_start = c_start + c_name.length + 1;//获取值的起点
			var c_end = document.cookie.indexOf(";", c_start);//获取结尾处
			if(c_end == -1) c_end = document.cookie.length;//如果是最后一个，结尾就是cookie字符串的结尾
			return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回
		}
	}
	return "";
}

